n=int(input())
kom=[]
count=0
kom_pr=[0]
if 999<n<10000:
      s=str(n)
      n1=int(s[0])
      n2=int(s[0]+s[1])
      n3=int(s[0]+s[1]+s[2])
      n4=int(s[0]+s[1]+s[2]+s[3])
      n5=int(s[1])
      n6=int(s[1]+s[2])
      n7=int(s[1]+s[2]+s[3])
      n8=int(s[2])
      n9=int(s[2]+s[3])
      n10=int(s[3])
      kom=[n1,n2,n3,n4,n5,n6,n7,n8,n9,n10]
      for i in kom:
            for d in range (1, 5001):
                  if i % d==0:
                        count+=1
            if count==2:
                  kom_pr.append(i)
            count=0
      print(max(kom_pr))
else:
      print (0)

